import 'dart:io';

void _limitLog(Object? object, [bool printToConsole = true, int length = 100]) {
  var str = object.toString();
  str = str.length <= length ? str : str.substring(0, length);
  if (printToConsole) stdout.writeln(str);
}

void _log(Object? object, [bool printToConsole = true]) {
  final str = object.toString();
  if (printToConsole) stderr.writeln(str);
}

void _error(Object? object,
    [StackTrace? s, String? title, bool printToConsole = true]) {
  if (title != null) _log('[$title]', printToConsole);
  _log('error: $object', printToConsole);
  if (s != null) {
    final isNotEmpty = s.toString().trim().isNotEmpty;
    final r = isNotEmpty ? s : StackTrace.current;
    _log('stackTrace: $r', printToConsole);
  } else {
    _log('stackTrace: ${StackTrace.current}', printToConsole);
  }
}

var log = _limitLog;
var error = _error;